var shopcart = new Vue({
    el: "#shopcart",
    data: {
        purchaseOrders: '',
        //购物车商品
        productInformation: [],
        //横向菜单
        crosswiseNav: [],
        //合计
        total: '',
        //商品数量
        Number: 0,
        //临时存储数据
        temporaryData: [],
    },
    //提交的时间方法
    methods: {
        //选择单个复选框
        onSelect: function (information) {
            var _this = this;
            var select = $('.items' + information.id).is(':checked');
            if (select) {
                _this.Number = select.length;
                _this.total = Number(_this.total) + Number(information.commoditySplPrc);
                _this.temporaryData.push(information);
            } else {
                $('#J_SelectAllCbx2').removeAttr("checked");
                for (var i = 0; i < _this.temporaryData.length; i++) {
                    if (information.id == _this.temporaryData[i].id) {
                        _this.temporaryData.splice(i, 1);
                        $.post("/home/order_detail/select", "", function (data) {
                            _this.productInformation = data.data.orderDetail;

                        });
                        break;
                    }
                }
            }
            _this.calculateTotalPrice();
        },
        //全选
        chooseAll: function () {
            var _this = this;
            var choose = $("input[type='checkbox']").length;
            if ($('#J_SelectAllCbx2').is(':checked')) {
                $("input[type='checkbox']").attr("checked", true);
                _this.temporaryData = _this.productInformation;
                _this.calculateTotalPrice();
            } else {
                $("input[type='checkbox']").removeAttr("checked");
                _this.temporaryData = [];
                _this.calculateTotalPrice();
            }
        },
        //删除购物车商品
        delete_Shop: function (information) {
            $.post("/home/order_detail/deleteShop",
                {"information": JSON.stringify(information)},
                function (data) {

                });
            window.location.href = window.location.href;
        },
        //加减法运算
        add: function (information) {
            var _this = this;
            var product = {};
            for (var i = 0; i < _this.productInformation.length; i++) {
                if (_this.productInformation[i].id == information.id) {
                    _this.productInformation[i].commodityNum = Number(_this.productInformation[i].commodityNum) + 1;
                    _this.productInformation[i].commoditySplPrc = ((information.commodityTrcPrc) * (_this.productInformation[i].commodityNum)).toFixed(2);
                    product = _this.productInformation[i];
                }
            }
            for (var j = 0; j < _this.temporaryData.length; j++) {
                if (information.id == _this.temporaryData[j].id) {
                    _this.temporaryData.splice(j, 1, product);
                    break;
                }

            }
            _this.calculateTotalPrice();

        },
        //计算总价工具类
        calculateTotalPrice: function () {
            var _this = this;
            _this.Number = _this.temporaryData.length;
            _this.total = 0;
            for (var i = 0; i < _this.temporaryData.length; i++) {
                _this.total += Number(_this.temporaryData[i].commoditySplPrc);
            }
        },
        //减法运算
        min: function (information) {
            var _this = this;
            var product = {};
            for (var i = 0; i < _this.productInformation.length; i++) {
                if (_this.productInformation[i].id == information.id) {
                    _this.productInformation[i].commodityNum = Number(_this.productInformation[i].commodityNum) - 1;
                    _this.productInformation[i].commoditySplPrc = ((_this.productInformation[i].commodityTrcPrc) * (_this.productInformation[i].commodityNum)).toFixed(2);
                    product = _this.productInformation[i];
                    if (_this.productInformation[i].commodityNum == 0) {
                        var detail = confirm("是否删除此商品");
                        if (detail) {
                            $.post("/home/order_detail/deleteShop",
                                {"information": JSON.stringify(information)},
                                function (data) {

                                });
                        }
                        window.location.reload();
                    }
                }
            }
            for (var i = 0; i < _this.temporaryData.length; i++) {
                if (_this.temporaryData[i].id == information.id) {
                    _this.total = _this.total - Number(_this.temporaryData[i].commodityTrcPrc);
                }

            }
        },


        /*  //结算功能
          account: function(){
              var _this = this;
              var select = $("input[name='item']");
              $.ajaxSetup({
                  async : false
              });
             for(var i=0;i<select.length;i++){
                 if($(select[i]).is(':checked')){
                     $.post("/home/order_detail/updateShop",{"results":JSON.stringify(_this.productInformation[i])},
                         function(data){
                         });
                 }
             }
          },*/

        //采购单
        insertOrders: function () {
            var _this = this;
            var f_id;
            var flag;
            var productInformations = new Array();
            var k = 0;
            var select = $("input[name='item']");
            for (var i = 0; i < select.length; i++) {
                if ($(select[i]).is(':checked')) {
                    productInformations[k] = _this.productInformation[i];
                    k++;
                }
            }
            if (k === 0) {
                _this.$notify({
                    message: "没有选中商品.",
                    type: 'error'
                });
                return;
            }
            $.ajaxSetup({
                async: false
            });
            debugger
            if (productInformations.length > 0) {
                _this.total
                $.post("/home/purchase_orders/insertDetailAndAddOrders4ShopCar", {"productInformation": JSON.stringify(productInformations),"total":_this.total},
                    function (data) {
                        if (data.code == 200) {
                            _this.f_id = data.data.f_id;
                            _this.flag = data.data.status;
                            _this.purchaseOrders = data.data.purchaseOrders;
                            if (_this.flag === "true") {
                                _this.$notify({
                                    message: "生成采购单成功.",
                                    type: 'success'
                                });
                            }
                        } else {
                            _this.$notify({
                                message: "生成采购单失败.",
                                type: 'error'
                            });
                            return;
                        }
                    });
            }
            if (_this.flag === "true") {
                _this.toJump(_this.f_id);
            }
        },
        toJump: function (f_id) {
            debugger
            var _this = this;

            $.ajaxSetup({
                async: false
            });
            $.post("/home/purchase_orders/selectPurchaseDetailedListAllByOrdersId",
                {"ordersId": _this.f_id},
                function (data) {
                    if (data.code == 200) {
                        //跳转添加采购单清单
                        $("#yryyr").load("/home/PurchaseAndOrdesdetail.html", function () {
                            PurchaseAndOrdesdetail.ordersDetails = data.data.purchaseDetailedList;
                            PurchaseAndOrdesdetail.urchaseAndOrdersdetail = _this.purchaseOrders;
                            PurchaseAndOrdesdetail.urchaseAndOrdersdetail.id = f_id;
                            PurchaseAndOrdesdetail.urchaseAndOrdersdetail.queryFlag=false;
                            PurchaseAndOrdesdetail.urchaseAndOrdersdetail.type="申请";
                        });
                    }
                });
        },
    },
    created: function () {
        var _this = this;
        var selecteds = $.post("/home/order_detail/select", "", function (data) {
            _this.productInformation = data.data.orderDetail;

        });
        //初始化横向菜单
        var crosswiseNavList = [
            {href: "designatedService.html", name: "定点服务", url: "#"},
            {href: "../home/offers.html", name: "询价表", url: "#"},

        ];
        _this.crosswiseNav = crosswiseNavList;//后台动态添加进入
    }
});
//页面加载后的初始方法


